var house = require("../../utils/houseWeb.js");
var collection = require("../../utils/collectionWeb.js");
var buying = require("../../utils/buyingWeb.js");

var status = require("../../utils/status.js");

Page({
  data: {
    // 是否模拟开盘
    isMoni: true,
    // 顶部绿条
    flagIsShow: true,
    fullTime: "",

    taskName: "",
    sellingStatus: "未开盘",

    // 切换开盘未开盘信息显示
    status_hidden: false,
    // 开盘时间
    selStartTime: "",
    // 当前批次
    currentBatchNo: 0,
    startNo: 1,
    endNo: 5,
    // 自己的批次
    myBatchNo: 1,
    // 自己的顺序号
    index: 1,

    timeTips: "选房限时",
    timeBackground: "rgba(242,242,242,1)",
    timeColor: "rgba(215,215,215,1)",
    // 每批次的选房时间
    // 当前批次剩余时间  分钟  秒
    minute: "",
    second: "",

    // 是否具有购买资格，是否已轮到自己购买
    canBuy: false,

    // 楼栋列表
    buildingNos: [],
    buildingSelectId: 0,
    // 单元列表
    unitNos: [],
    unitSelectId: 0,
    // 房间列表
    roomList: [],
    // 切换房间列表时的时间戳
    roomChange: 0,

    // 选房还剩最后30秒警告
    lastWarning: false,

    // 跳转到下一页面需要传的参数
    houseId: 0,
    buyerName: "",
    certifyCardType: "身份证",
    cardId: "",
    houseNo: "",
    // 收藏列表
    collectionList: [],
    collectionModalOpened: false,
    // 是否能第二次点击
    secondClick: true,

    // 订单列表
    orderList: [],
    orderEmpty: false,

    tabIcon1: "../../image/tab11.png",
    tabIcon2: "../../image/tab20.png",
    tabIcon3: "../../image/tab30.png",
    hidden1: false,
    hidden2: true,
    hidden3: true,

    taskType: "房"
  },
  onLoad() {
    // this.setData({
    //   // 是否模拟开盘
    //   isMoni: my.getStorageSync({ key: 'isMoni' }).data
    // });
    var taskType = "房";
    if (my.getStorageSync({ key: 'taskType' }).data == "车位") {
      taskType = "车位";
      my.setNavigationBar({ title: "选车位大厅" });
    }
    var certifyCardType1 = my.getStorageSync({ key: 'certifyCardType' }).data;
    var certifyCardType = "居民身份证";
    if (certifyCardType1 == "0") {
      certifyCardType = "居民身份证";
    } else if (certifyCardType1 == "B") {
      certifyCardType = "港澳通行证";
    } else if (certifyCardType1 == "C") {
      certifyCardType = "台湾居民来往大陆通行证";
    } else {
      certifyCardType = "护照";
    }

    var taskInfo = my.getStorageSync({ key: 'taskInfo' }).data;
    this.setData({
      taskName: taskInfo.taskName,
      selStartTime: taskInfo.selStartTime,
      buyerName: my.getStorageSync({ key: 'nameInputValue' }).data,
      certifyCardType: certifyCardType,
      cardId: my.getStorageSync({ key: 'idcardInputValue' }).data,
      taskType: taskType,
      timeTips: "选" + taskType + "限时"
    });
  },
  // 结束下拉刷新
  onPullDownRefresh() {
    my.stopPullDownRefresh();
  },
  onShow() {
    house.client.getBuildingList(this);

    status.client.openInfoAsk(this);
  },
  buildingClick(event) {
    var id = event.currentTarget.dataset.id; //获取data-id
    if (this.data.buildingSelectId != id) {
      var id0 = this.data.buildingSelectId;
      var id1 = this.data.unitSelectId;
      this.setData({
        ["buildingNos[" + id0 + "].background"]: "rgba(255,255,255,1)",
        ["buildingNos[" + id0 + "].color"]: "rgba(51,51,51,1)",
        ["buildingNos[" + id + "].background"]: "rgba(38,128,235,1)",
        ["buildingNos[" + id + "].color"]: "rgba(255,255,255,1)",
        ["unitNos[" + id1 + "].background"]: "rgba(255,255,255,1)",
        ["unitNos[" + id1 + "].color"]: "rgba(51,51,51,1)",
        buildingSelectId: id,
        unitSelectId: 0
      });
      house.client.getUnitList(this.data.buildingNos[id].buildingNo, this);
    }
  },
  unitClick(event) {
    var id = event.currentTarget.dataset.id; //获取data-id
    if (this.data.unitSelectId != id) {
      var id0 = this.data.unitSelectId;
      this.setData({
        ["unitNos[" + id0 + "].background"]: "rgba(255,255,255,1)",
        ["unitNos[" + id0 + "].color"]: "rgba(51,51,51,1)",
        ["unitNos[" + id + "].background"]: "rgba(38,128,235,1)",
        ["unitNos[" + id + "].color"]: "rgba(255,255,255,1)",
        unitSelectId: id
      });
      house.client.getRoomList(
        this.data.buildingNos[this.data.buildingSelectId].buildingNo,
        this.data.unitNos[id].unitNo,
        this
      );
    }
  },
  roomClick(event) {
    var id = event.currentTarget.dataset.id; //获取data-id
    // status 0 没人选购，1-已经锁定，2-已经被认购，3-本人锁定，4-本人认购
    if (this.data.roomList[id].status == 1) {
      // 当前房源被别人锁定
      my.showToast({ content: "当前" + this.data.taskType + "已被其他购" + this.data.taskType + "者锁定，请先选择其他" + this.data.taskType });
    } else if (this.data.roomList[id].status == 2) {
      // 当前房源已售，status 2-已经被认购
      my.showToast({ content: "当前" + this.data.taskType + "已售，请考虑其他" + this.data.taskType });
    } else if (this.data.roomList[id].status == 3) {
      // 当前房源被自己锁定时，直接进入购房确认页
      my.navigateTo({ url: "../confirmBuy/confirmBuy?houseId=" + this.data.roomList[id].id });
    } else if (this.data.roomList[id].status == 4) {
      // 当前房源被自己认购
      my.showToast({ content: "您已认购此" + this.data.taskType });
    } else {
      // 当前房源可选
      my.navigateTo({
        url: "../buying/buying?houseId=" + this.data.roomList[id].id + "&houseNo=" + this.data.roomList[id].houseNo +
          " " + this.data.roomList[id].buildingArea + "m²&certifyCardType=" + this.data.certifyCardType +
          "&totalPrice=" + this.data.roomList[id].totalPrice + "&isCollected=" + this.data.roomList[id].isCollected +
          "&canBuy=" + this.data.canBuy + "&minute=" + this.data.minute + "&second=" + this.data.second
      });
    }
  },
  // 选房时间最后30秒提醒关闭
  closeWarning() {
    this.setData({ lastWarning: false });
  },
  // 向上收藏顺序
  upCollectionOrder(event) {
    var id = event.currentTarget.dataset.id; //获取data-id
    var houseIdList = new Array();
    houseIdList.push(this.data.collectionList[id].id);
    for (var i = 0; i < this.data.collectionList.length; i++) {
      if (i != id) {
        houseIdList.push(this.data.collectionList[i].id);
      }
    }
    collection.client.upCollectionOrder(houseIdList, this);
  },
  // 收藏列表点击认购
  collectionBuy(event) {
    var id = event.currentTarget.dataset.id; //获取data-id
    if (this.data.canBuy) {
      // status 0 没人选购，1-已经锁定，2-已经被认购
      // 当前房源已售，status 2-已经被认购
      // 收藏列表并无status，TODO
      console.log("this.data.collectionList[id].status: " + this.data.collectionList[id].status);
      console.log("this.data.collectionList[id].id : " + this.data.collectionList[id].id);
      console.log("my.getStorageSync({ key: 'lockedHouseId' }).data: " + my.getStorageSync({ key: 'lockedHouseId' }).data);
      if (this.data.collectionList[id].status == 2) {
        my.showToast({ content: "当前" + this.data.taskType + "已售，请考虑其他" + this.data.taskType, duration: 2000 });
      } else if (this.data.collectionList[id].status == 1) {
        // 当前房源已被锁定，status 1-已经锁定
        if (this.data.collectionList[id].id == my.getStorageSync({ key: 'lockedHouseId' }).data) {
          // 当前房源被自己锁定时，直接进入购房确认页
          my.navigateTo({ url: "../confirmBuy/confirmBuy?houseId=" + this.data.collectionList[id].id });
        } else {
          // 当前房源被别人锁定
          my.showToast({ content: "当前" + this.data.taskType + "已被其他购" + this.data.taskType + "者锁定，请先选择其他" + this.data.taskType, duration: 2000 });
        }
      } else {
        if (this.data.collectionList[id].id == my.getStorageSync({ key: 'lockedHouseId' }).data) {
          // 当前房源被自己锁定时，直接进入购房确认页
          my.navigateTo({ url: "../confirmBuy/confirmBuy?houseId=" + this.data.collectionList[id].id });
        } else {
          // 当前房源可选，打开对话框
          this.setData({
            buyerName: this.data.buyerName,
            houseNo: this.data.collectionList[id].houseNo + " " + this.data.collectionList[id].buildingArea + "m²",
            totalPrice: this.data.collectionList[id].totalPrice,
            collectionModalOpened: true,
            houseId: this.data.collectionList[id].id
          });
        }
      }
    } else {
      var taskId = my.getStorageSync({ key: 'taskId' }).data;
      var buyerStatus = my.getStorageSync({ key: 'buyerStatus' + taskId }).data;
      var toastString = "未轮到您的购买批次，请耐心等待";
      if (buyerStatus == 2) {
        toastString = "您已放弃选" + this.data.taskType + "，不能再次选" + this.data.taskType;
      } else if (buyerStatus == 3) {
        toastString = "您已认购您的" + this.data.taskType + "，不能再次选" + this.data.taskType;
      } else if (buyerStatus == 4) {
        toastString = "此楼盘选" + this.data.taskType + "已结束，不能再次选" + this.data.taskType;
      }
      my.showToast({ content: toastString, duration: 2000 });
    }
  },
  collectionSubmit() {
    buying.client.submitBuy(this.data.houseId, this);
  },
  collectionClose() {
    this.setData({ collectionModalOpened: false });
  },
  // 删除收藏接口
  deleteCollection(event) {
    var id = event.currentTarget.dataset.id; //获取data-id
    collection.client.deleteCollection(this.data.collectionList[id].id, this);
  },
  orderDetail(event) {
    var id = event.currentTarget.dataset.id; //获取data-id
    console.log(this.data.orderList[id].orderType);
    if (this.data.orderList[id].orderType == "线上支付") {
      my.navigateTo({
        url: "../orderDetail/orderDetail?orderNo=" +
          this.data.orderList[id].orderNo
      });
    } else if (this.data.orderList[id].orderType == "线下支付") {
      my.navigateTo({
        url: "../orderDetailOffline/orderDetailOffline?orderNo=" +
          this.data.orderList[id].orderNo
      });
    } else {
      // 如果支付金额为0，则为0订单，进入0订单页面
      if (this.data.orderList[id].payAmount == 0) {
        my.navigateTo({
          url: "../orderDetail/orderDetail?orderNo=" +
            this.data.orderList[id].orderNo
        });
      } else {
        // 其他情况为未指定支付方式 需要重新指定支付方式
        my.navigateTo({
          url: "../selectSuccess/selectSuccess?orderNo=" +
            this.data.orderList[id].orderNo
        });
      }
    }
  },
  // 点击下面的tab
  tab1() {
    this.setData({
      tabIcon1: "../../image/tab11.png",
      tabIcon2: "../../image/tab20.png",
      tabIcon3: "../../image/tab30.png",
      hidden1: false,
      hidden2: true,
      hidden3: true
    });
    my.setNavigationBar({ title: "选" + this.data.taskType + "大厅" });
    house.client.getBuildingList(this);
  },
  tab2() {
    this.setData({
      tabIcon1: "../../image/tab10.png",
      tabIcon2: "../../image/tab21.png",
      tabIcon3: "../../image/tab30.png",
      hidden1: true,
      hidden2: false,
      hidden3: true
    });
    my.setNavigationBar({ title: "我的收藏" });
    collection.client.getCollectionList(this);
  },
  tab3() {
    this.setData({
      tabIcon1: "../../image/tab10.png",
      tabIcon2: "../../image/tab20.png",
      tabIcon3: "../../image/tab31.png",
      hidden1: true,
      hidden2: true,
      hidden3: false
    });
    my.setNavigationBar({ title: "我的订单" });
    house.client.getOrderList(this);
  },
  onShareAppMessage() {
    return {
      title: '公证 e 选房',
      desc: '提供在线选房服务',
      path: 'pages/login/login'
    };
  }
});
